package org.club.common.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; // 引入 SLF4J 日志工厂
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //  logger 实例
    private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .cors()  // 启用CORS
            .and()
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/club/**").permitAll()
            .antMatchers("OPTIONS", "/**").permitAll()  // 允许所有OPTIONS请求通过
            .anyRequest().authenticated();
    }

    // 基于PBKDF2算法的密码编码器
    @Bean
    public PasswordEncoder passwordEncoder() {
        String s = "c9c2ed47782c26fdc3926c381efa1b6992b0cd6ffbf97b56e6ac2dd3ee3c28b7";
        logger.info("盐值:{}", s);
        return new Pbkdf2PasswordEncoder(s, 100000, 256);
    }
}
